package com.opensourcebolivia.navidar;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensourcebolivia.navidar.service.EncargadoDAO;

public class ServletCreateEncargado extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		System.out.println("Creando una nuevo encargado");
		User user = (User) req.getAttribute("user");
		if (user == null) {
			UserService userService = UserServiceFactory.getUserService();
			user = userService.getCurrentUser();
		}

		String nombres = checkNull(req.getParameter("nombres"));
		String apellidoP = checkNull(req.getParameter("apellidoP"));
		String apellidoM = checkNull(req.getParameter("apellidoM"));
		String telefono = checkNull(req.getParameter("telefono"));
		String celular = checkNull(req.getParameter("celular"));
		String direccion = checkNull(req.getParameter("direccion"));
		String email = checkNull(req.getParameter("email"));

		EncargadoDAO.INSTANCE.add(nombres, apellidoP, apellidoM, telefono, celular, direccion, email);

		resp.sendRedirect("/EncargadoApplication.jsp");
	}

	private String checkNull(String s) {
		if (s == null) {
			System.out.println("Asignando vacio a nulo");
			return "";
		}
		return s;
	}
}